package com.pignest.hotel.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pignest.hotel.beans.Dictionary;
import com.pignest.hotel.dao.DictionaryDao;
import com.pignest.hotel.system.L;

public class DictionaryService extends BaseService {

	private DictionaryDao dictionaryDao;
	
	public void setDictionaryDao(DictionaryDao dictionaryDao) {
		this.dictionaryDao = dictionaryDao;
	}
	
	public List<Dictionary> listAll() {
		return dictionaryDao.listAll(Dictionary.class);
	}
	
	public Dictionary getDictionary(String id) {
		return dictionaryDao.get(Dictionary.class, id);
	}
	
	public String getValue(String key) {
		List<String> dicts = listValues(key);
		if (dicts == null || dicts.isEmpty())
			return null;
		return dicts.get(0);
	}
	
	public Dictionary getDictionaryByKey(String key) {
		return get(Dictionary.class, "key", key);
	}
	
	public String getValue(String key, String def) {
		List<String> dicts = listValues(key);
		if (dicts == null || dicts.isEmpty())
			return def;
		return dicts.get(0);
	}

	
	public List<String> listValues(String key) {
		return dictionaryDao.getValues(key);
	}
	
	public List<Dictionary> listDictionary(String key) {
		return query(Dictionary.class, "key=?", new Object[] {key}, null);
	}
	
	public List<String> listValuesByPerfix(String perfix) {
		return dictionaryDao.getValuesByPerfix(perfix);
	}
	
	public List<Dictionary> listDictionaries(String key){
		return dictionaryDao.getDictionaries(key);
	}
	
	public void addDictionary(Dictionary dict) {
		dictionaryDao.insert(dict);
	}
	
	public void updateDictionary(Dictionary dict) {
		dictionaryDao.update(dict);
	}
	
	public void saveDictionary(String key, String value) {
		Dictionary d = getDictionaryByKey(key);
		if (d != null) {
			if (StringUtils.isEmpty(value)) {
				delete(d);
				L.delete(d);
			} else {
				d.setValue(value);
				update(d);
				L.update(d);
			}
		} else {
			d = new Dictionary();
			d.setKey(key);
			d.setValue(value);
			insert(d);
			L.insert(d);
		}
	}
	
	public void saveDictionaries(String perfix, String values) {
		String vs = values.trim();
		if (vs.length() < 2 || vs.charAt(0) != '[' || vs.charAt(vs.length() - 1) != ']')
			return;
		vs = vs.substring(1, vs.length() - 1);
		String vv[] = StringUtils.split(vs, ',');
		dictionaryDao.deleteByPerfix(perfix);
		int n = 0;
		for (int i = 0; i < vv.length; i++) {
			vv[i] = vv[i].trim();
			if (StringUtils.isEmpty(vv[i]))
				continue;
			n ++;
			if (n > 99)
				break;
			String key = String.format("%s%02d", perfix, n);
			saveDictionary(key, vv[i]);
		}
	}
	
	public void deleteDictionary(Dictionary dict) {
		dictionaryDao.delete(dict);
	}
	
	public void deleteDictionary(String dictId) {
		deleteDictionary(dictionaryDao.get(Dictionary.class, dictId));
	}
	
	public void deleteDictionaries(String[] dictIds) {
		if (dictIds == null)
			return;
		for (int i = 0; i < dictIds.length; i++) {
			deleteDictionary(dictIds[i]);
		}
	}
	
	public void deleteDictionaryByKey(String key) {
		dictionaryDao.delete(key);
	}
	
	public boolean exists(String key, String value) {
		return dictionaryDao.exists(key, value);
	}
	
	public int getCount(String key) {
		return dictionaryDao.getCount(key);
	}
}
